using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Project.EPMSync.Config.Library;
using Microsoft.Office.Project.EPMSync.Config.Editor.Properties;

namespace Microsoft.Office.Project.EPMSync.Config.Editor
{
    public partial class FormBuildConnectionString : Form
    {
        private FormMain _ptrMain;
        private SqlConnection _sqlConnection;

        public FormBuildConnectionString(FormMain main, string sqlConnectionString)
        {
            InitializeComponent();
            _ptrMain = main;
            try
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(sqlConnectionString);

                textBoxServerName.Text = builder.DataSource;
                textBoxDatabaseName.Text = builder.InitialCatalog;
                if (builder.IntegratedSecurity)
                {
                    radioButtonWindowsAuth.Checked = true;
                    WindowsEnabled();
                }
                else
                {
                    radioButtonSqlAuth.Checked = true;
                    SqlEnabled();
                    textBoxUserName.Text = builder.UserID;
                    textBoxPassword.Text = builder.Password;
                }
            }
            catch(Exception)
            {
                RightAwareMessageBox.Show(Resources.ConnectionStringInitFailed, Resources.EditorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void WindowsEnabled()
        {
            labelPassword.Enabled = false;
            labelUserName.Enabled = false;
            textBoxPassword.Enabled = false;
            textBoxUserName.Enabled = false;
        }

        private void SqlEnabled()
        {
            labelPassword.Enabled = true;
            labelUserName.Enabled = true;
            textBoxPassword.Enabled = true;
            textBoxUserName.Enabled = true;
       }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            string connectionString = ConnectionString;
            _ptrMain.UpdateSqlConnectionString(connectionString);
            if (!string.IsNullOrEmpty(connectionString))
                this.Close();
        }

        private void buttonTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionString = ConnectionString;
                if (!string.IsNullOrEmpty(connectionString))
                {
                    _sqlConnection = new SqlConnection(connectionString);
                    _sqlConnection.Open();
                    RightAwareMessageBox.Show(Resources.ConnectionTestSuccess, Resources.EditorTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                RightAwareMessageBox.Show(Resources.ConnectionTestFailed, Resources.EditorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                _sqlConnection.Close();
            }

        }

        private string ConnectionString
        {
            get
            {
                try
                {
                    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                    builder.DataSource = textBoxServerName.Text;
                    builder.InitialCatalog = textBoxDatabaseName.Text;
                    if (radioButtonWindowsAuth.Checked)
                        builder.IntegratedSecurity = true;
                    else
                    {
                        builder.IntegratedSecurity = false;
                        builder.UserID = textBoxUserName.Text;
                        builder.Password = textBoxPassword.Text;
                    }
                    return builder.ConnectionString;
                }
                catch (Exception)
                {
                    RightAwareMessageBox.Show(Resources.ConnectionStringInitFailed, Resources.EditorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return string.Empty;
                }

            }

        }

        private void radioButtonWindowsAuth_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonWindowsAuth.Checked)
                WindowsEnabled();
            else
                SqlEnabled();
        }
    }
}